package me.cxlr.qinlauncher2.dao;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmResults;
import me.cxlr.qinlauncher2.model.Action;
import me.cxlr.qinlauncher2.model.DockAction;
import me.cxlr.qinlauncher2.model.KeyAction;

/**
 * DockActionDao
 *
 * @author 酷安用户:山轮纸
 */
public class DockActionDao {
    private Realm realm;

    /**
     * 保存
     *
     * @param dockActionList Dock
     */
    public void saveDockAction(ArrayList<DockAction> dockActionList) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        realm.beginTransaction();
        List<DockAction> tmpList = realm.copyToRealm(dockActionList);
        realm.commitTransaction();
        realm.close();
    }

    /**
     * 修改DockAction
     *
     * @param dockAction DockAction
     */
    public void updateDockAction(DockAction dockAction) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        realm.beginTransaction();
        DockAction tmp = realm.copyToRealmOrUpdate(dockAction);
        realm.commitTransaction();
        realm.close();
    }

    /**
     * 修改DockAction
     *
     * @param dockActionList List<DockAction>
     */
    public void updateDockAction(List<DockAction> dockActionList) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        realm.beginTransaction();
        List<DockAction> tmpList = realm.copyToRealmOrUpdate(dockActionList);
        realm.commitTransaction();
        realm.close();
    }

    /**
     * 查询全部Dock
     *
     * @return 全部Dock
     */
    public List<DockAction> findAllDockAction() {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        List<DockAction> results = realm.where(DockAction.class).findAll();
        assert results != null;
        List<DockAction> tmp = realm.copyFromRealm(results);
        realm.close();
        return tmp;
    }

    /**
     * 根据图标数量和图标名称查询
     *
     * @param iconNumber 图标个数 3或5
     * @param iconName   名字
     * @return 托盘动作
     */
    public DockAction findDockActionByIconNumberAndIconName(int iconNumber, String iconName) {
        realm = Realm.getInstance(RealmConfig.getRealmConfig());
        DockAction tmp = realm.where(DockAction.class)
                .equalTo("iconNumber", iconNumber)
                .and()
                .equalTo("iconName", iconName)
                .findFirst();
        DockAction dockAction = realm.copyFromRealm(tmp);
        realm.close();
        return dockAction;
    }

    /**
     * 根据包名查询DockAction
     *
     * @param packageName 包名
     * @return List<DockAction>
     */
    public List<DockAction> findDockActionByPackageName(String packageName) {
        List<DockAction> dockActionList = new ArrayList<>(16);
        try {
            realm = Realm.getInstance(RealmConfig.getRealmConfig());
            RealmResults<DockAction> results = realm.where(DockAction.class)
                    .equalTo("app.packageName", packageName)
                    .findAll();
            if (results != null) {
                dockActionList = realm.copyFromRealm(results);
            }
            return dockActionList;
        } catch (Exception e) {
            Logger.e("findDockActionByPackageName:" + e.toString());
            return dockActionList;
        } finally {
            realm.close();
        }
    }
}
